package com.woniu.controller;


import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.woniu.entity.Client;
import com.woniu.entity.Product;
import com.woniu.entity.SecondTrial;
import com.woniu.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 处理产品列表请求
 */

@Api(value = "产品列表", tags = {"产品管理"})
@RestController
@RequestMapping("/product")
public class ProductController extends BaseController {
    @Autowired
    ProductService productService;



    @ApiOperation(value = "查询所有的产品列表",response= Product.class, httpMethod="GET", tags = {"产品管理"})
    @ApiImplicitParam(name = "product",value = "封装的产品列表实体类",required = true)
    @GetMapping("/findAll")
    public TableDataInfo findAll(Product product) {
        startPage();
        List<Product> all = productService.findAll(product);
        return getDataTable(all);

    }
    @ApiOperation(value = "导出产品列表",response= Product.class, httpMethod="GET", tags = {"产品管理"})
    @PreAuthorize("@ss.hasPermi('test:export')")
    @Log(title = "产品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Product product) throws IOException {
        List<Product> list = productService.exportList();
        ExcelUtil<Product> util = new ExcelUtil<>(Product.class);
        util.exportExcel(response, list, "产品列表信息");
    }

    @ApiOperation(value = "增加产品信息",response= Product.class, produces="application/json", httpMethod="POST", tags = {"产品管理"})
    @PostMapping("/addProduct")
    @ResponseBody
    public AjaxResult addProduct(@RequestBody Product product) {

        return productService.addProduct(product);
    }


    @ApiOperation(value = "通过产品id查询单条产品信息",response= Integer.class, httpMethod="GET", tags = {"产品管理"})
    @GetMapping("/findOne/{productId}")
    public AjaxResult findOne(@PathVariable Integer productId) {
        try {
            Product one = productService.findOne(productId);
            return new AjaxResult(HttpStatus.SUCCESS,"查询成功",one);
        }catch (Exception e){
            throw new RuntimeException();
        }
    }
}